package com.baibei.ebec.welcome;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import com.baibei.crossboarder.R;
import com.baibei.ebec.guide.AdvertisementActivity;
import com.baibei.ebec.guide.GuideActivity;
import com.baibei.ebec.home.Rotate3dAnimation;
import com.baibei.module.AppRouter;

import butterknife.BindView;
import butterknife.ButterKnife;


public class WelcomeActivity extends AppCompatActivity {

    @BindView(R.id.iv_logo)
    ImageView mIvLogo;
    @BindView(R.id.tv_name)
    TextView mTvName;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_welcome);
        ButterKnife.bind(this);
        //开始动画
        mIvLogo.post(new Runnable() {
            @Override
            public void run() {
                startAnimiate();
            }
        });
    }


    private void startAnimiate() {
        AnimationSet animationSet = new AnimationSet(true);
        AlphaAnimation alphaAnimation = new AlphaAnimation(0f, 1f);
        alphaAnimation.setDuration(1000);
        animationSet.addAnimation(alphaAnimation);

        float centerX = mIvLogo.getWidth() / 2.0f;
        float centerY = mIvLogo.getHeight() / 2.0f;
        float centerZ = 0f;
        Rotate3dAnimation rotate3dAnimationX = new Rotate3dAnimation(0, 360, centerX, centerY, centerZ, Rotate3dAnimation.ROTATE_Y_AXIS, true);
        rotate3dAnimationX.setInterpolator(new LinearInterpolator());
        rotate3dAnimationX.setDuration(2000);
        rotate3dAnimationX.setStartOffset(1000);

        animationSet.addAnimation(rotate3dAnimationX);
        animationSet.setAnimationListener(new Animation.AnimationListener() {


            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
//                routeToMain();
//                routeToAdvert();
                routetoGuide();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        mIvLogo.startAnimation(animationSet);
        mTvName.startAnimation(alphaAnimation);
    }

    private void routetoGuide() {
        Intent intent = new Intent(WelcomeActivity.this, GuideActivity.class);
        startActivity(intent);
        finish();
    }

    private void routeToAdvert() {
        Intent intent = new Intent(WelcomeActivity.this, AdvertisementActivity.class);
        startActivity(intent);
        finish();
    }

    private void routeToMain() {
        AppRouter.routeToMain(WelcomeActivity.this);
        finish();
    }

}
